#!/bin/bash
# Turtle Dashboard installieren · © 2026 MOVIE TURTLES GERMANY
# Baut das Dashboard aus dem mitgelieferten Quellcode – ohne Terminal.
export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/homebrew/bin"
HERE="$(cd "$(dirname "$0")/.." && pwd)"
SRC="$HERE/Resources/Source"
LOG="$HOME/Library/Logs/Turtle Dashboard Installation.log"
WORK="$(mktemp -d /tmp/TurtleDashboard.XXXXXX)"
APPNAME="Turtle Dashboard.app"
: > "$LOG"

say() { osascript -e "display notification \"$1\" with title \"Turtle Dashboard\" subtitle \"MOVIE TURTLES GERMANY\"" >/dev/null 2>&1; }
alert() { osascript -e "display dialog \"$1\" with title \"Turtle Dashboard\" buttons {\"OK\"} default button 1 with icon $2" >/dev/null 2>&1; }
fail() {
  alert "$1\n\nDetails stehen in: Library › Logs › Turtle Dashboard Installation.log" stop
  open -R "$LOG" 2>/dev/null
  rm -rf "$WORK"; exit 1
}

# 1) Xcode vorhanden und eingerichtet?
DEV="$(xcode-select -p 2>/dev/null)"
if [ ! -d /Applications/Xcode.app ] && [ -z "$DEV" ]; then
  osascript -e 'display dialog "Für die Installation brauchst du einmalig Xcode (kostenlos im App Store).\n\nInstallieren, einmal öffnen, dann hier nochmal doppelklicken." with title "Turtle Dashboard" buttons {"Abbrechen", "App Store öffnen"} default button 2 with icon caution' >/dev/null 2>&1 \
    && open "macappstore://apps.apple.com/app/xcode/id497799835"
  exit 0
fi
if [ -d /Applications/Xcode.app ] && { [[ "$DEV" != /Applications/Xcode.app* ]] || ! xcodebuild -license check >/dev/null 2>&1; }; then
  osascript -e 'do shell script "xcode-select -s /Applications/Xcode.app/Contents/Developer && xcodebuild -license accept && xcodebuild -runFirstLaunch" with administrator privileges with prompt "Turtle Dashboard richtet Xcode einmalig ein."' >>"$LOG" 2>&1 \
    || fail "Xcode konnte nicht eingerichtet werden. Öffne Xcode einmal und lass die Zusatzkomponenten installieren, dann nochmal versuchen."
fi

# 2) Bauen
say "Baue das Dashboard … das dauert 1–3 Minuten."
cp -R "$SRC/." "$WORK/" || fail "Quellcode konnte nicht kopiert werden."
cd "$WORK" || fail "Arbeitsordner fehlt."
swift build -c release >>"$LOG" 2>&1 || fail "Beim Bauen ist ein Fehler passiert. Schick Claude bitte die Log-Datei (wird gleich im Finder gezeigt)."
BIN="$(swift build -c release --show-bin-path 2>>"$LOG")/TurtleDashboard"
[ -f "$BIN" ] || fail "Die fertige App wurde nicht gefunden."

# 3) App zusammenbauen
OUT="$WORK/out/$APPNAME"
mkdir -p "$OUT/Contents/MacOS" "$OUT/Contents/Resources"
cp "$BIN" "$OUT/Contents/MacOS/TurtleDashboard"
cp Resources/Info.plist "$OUT/Contents/Info.plist"
cp Resources/*.png "$OUT/Contents/Resources/"
cp "$HERE/Resources/AppIcon.icns" "$OUT/Contents/Resources/AppIcon.icns"
# Feste Kennung in der Signatur: macOS merkt sich "Erlauben" (Kalender, Mail, Musik) dann auch über Updates hinweg
BID=$(/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" Resources/Info.plist)
codesign --force --deep --sign - -r="designated => identifier \"$BID\"" "$OUT" >>"$LOG" 2>&1 \
  || codesign --force --deep --sign - "$OUT" >>"$LOG" 2>&1

VERSION=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" Resources/Info.plist)
SHARE="$HOME/Desktop/Turtle Dashboard $VERSION.zip"
rm -f "$SHARE"
ditto -c -k --keepParent "$OUT" "$SHARE" >>"$LOG" 2>&1

# 4) Installieren und starten
osascript -e 'quit app "Turtle Dashboard"' >/dev/null 2>&1
sleep 1
rm -rf "/Applications/$APPNAME"
ditto "$OUT" "/Applications/$APPNAME" >>"$LOG" 2>&1 || fail "Konnte nicht in den Programme-Ordner kopieren."
rm -rf "$WORK"
say "Fertig! Version $VERSION ist installiert. Fürs Team liegt eine Zip auf dem Schreibtisch."
open "/Applications/$APPNAME"
